package com.freight.file.serviceimpl;



import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImageWatermark {

            public static final String MARK_TEXT="AAAA";  //字体水印
    public static final String FONT_NAME="微软雅黑";
    public static final int FONT_STYLE= Font.BOLD;
//    public static final int FONT_SIZE=20;
    public static final Color FONT_COLOR=Color.red;
    public static final  int X=10;
    public static final int Y=10;
    public static final float ALPHA=0.3f;  //透明度

    //设置字体大小
    public static final int FONT_SIZE = 120;



    public  String watermark(File img, String imgFileName, String imgpath, String realImgPath) {
        //创建图像编码工具
        FileOutputStream os = null;
        String logoFileName = "logo_" + imgFileName;
        try {
            //获取原图大小
            Image image = ImageIO.read(img);
            int width = image.getWidth(null);
            int height = image.getHeight(null);
            BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = bufferedImage.createGraphics();
            graphics.drawImage(image, 0, 0, width, height, null);

            //设置文字水印
            graphics.setFont(new Font(FONT_NAME, FONT_STYLE, FONT_SIZE));
            graphics.setColor(FONT_COLOR);

            //获取文本水印的真正高度与宽度
            int wid = FONT_SIZE * getLength(MARK_TEXT);
            int hei = FONT_SIZE;

            //设置透明度
            graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));
            //旋转图片
            graphics.rotate(Math.toRadians(30), bufferedImage.getWidth() / 2, bufferedImage.getHeight() / 2);
            int x = -width / 2;
            int y = FONT_SIZE;
            while (x < width * 1.5) {
                y = -height / 2;
                while (y < height * 1.5) {
                    graphics.drawString(MARK_TEXT, x, y);
                    y += hei + y;
                }
                x += wid + x;
            }
            graphics.dispose();

            os = new FileOutputStream(realImgPath + "/" + logoFileName);
//            JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os);
            ImageIO.write(bufferedImage, "jpg", os);
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return imgpath + "/" + logoFileName;
    }



    //计算文本长度
    public  int getLength(String text) {
        int length = text.length();
        for (int i = 0; i < length; i++) {
            String s = String.valueOf(text.charAt(i));
            if (s.getBytes().length > 1) length++;
        }
        length = length % 2 == 0 ? length / 2 : length / 2 + 1;
        return length;
    }



    /**
     * 给图片添加多个文字水印、可设置水印文字旋转角度
     * source 需要添加水印的图片路径
     * outPut 添加水印后图片输出路径
     * imageName 图片名称
     * imageType 图片类型
     * color 水印文字的颜色
     * word 水印文字
     * degree 水印文字旋转角度，为null表示不旋转
     */
    public File markImageByMoreText(File file, String outputPath) {
        try {


            //获取源图像的宽度、高度
            Image image = ImageIO.read(file);
            int width = image.getWidth(null);
            int height = image.getHeight(null);

            BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

            //创建绘图工具对象
            Graphics2D graphics2D = bufferedImage.createGraphics();
            //其中的0代表和原图位置一样
            graphics2D.drawImage(image, 0, 0, width, height, null);

            //设置水印文字（设置水印字体样式、粗细、大小）
            graphics2D.setFont(new Font(FONT_NAME, FONT_STYLE, FONT_SIZE));

            //设置水印颜色
            graphics2D.setColor(Color.red);

            //设置水印透明度
            graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));

//            //设置水印旋转
//            if (null != degree) {
//                graphics2D.rotate(Math.toRadians(degree), (double) bufferedImage.getWidth() / 2, (double) bufferedImage.getHeight() / 2);
//            }

            int x = width / 3;
            int y = FONT_SIZE;
            int space = height / FONT_SIZE;
//            for (int i = 0; i < space; i++) {
//                //如果最后一个坐标的y轴比height高，直接退出
//                if ((y + FONT_SIZE) > height) {
//                    break;
//                }
                //进行绘制
            graphics2D.drawString("ASDF", x, y);
//                y += (2 * FONT_SIZE);
//            }

            graphics2D.dispose();

            //输出图片
            File sf = new File(outputPath, "1" + ".jpg"  );

            return sf;



        } catch (Exception e) {
            e.printStackTrace();

        }
        return null;

    }

}
